package intensivecomp.examples;

import intensivecomp.mapred.Map;
import intensivecomp.mapred.Reduce;
import intensivecomp.util.CustomerNLineInputFormat;
import intensivecomp.util.CustomerWholeFileInputFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCountExample {
    public WordCountExample() {
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //conf.set("mapreduce.input.lineinputformat.linespermap","2");
        String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }

        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCountExample.class);
        job.setInputFormatClass(CustomerWholeFileInputFormat.class);
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);
        CustomerWholeFileInputFormat.setInputDirRecursive(job, true);
        for(int i = 0; i < otherArgs.length - 1; ++i) {
            CustomerWholeFileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }

        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}
